/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package observertest;

import java.util.List;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

/**
 *
 * @author Student
 */
public class Board extends JPanel {
    
    private boolean placingRocks, placingFlowers, placingCritters, placingBugs, placingNulls,  placingDefaults;

    //<editor-fold defaultstate="collapsed" desc="get set methods">
    public boolean isPlacingBugs() {
        return placingBugs;
    }
    
    public void setPlacingBugs(boolean placingBugs) {
        this.placingBugs = placingBugs;
    }
    
    public boolean isPlacingCritters() {
        return placingCritters;
    }
    
    public void setPlacingCritters(boolean placingCritters) {
        this.placingCritters = placingCritters;
    }
    
    public boolean isPlacingDefaults() {
        return placingDefaults;
    }
    
    public void setPlacingDefaults(boolean placingDefaults) {
        this.placingDefaults = placingDefaults;
    }
    
    public boolean isPlacingFlowers() {
        return placingFlowers;
    }
    
    public void setPlacingFlowers(boolean placingFlowers) {
        this.placingFlowers = placingFlowers;
    }
    
    public boolean isPlacingNulls() {
        return placingNulls;
    }
    
    public void setPlacingNulls(boolean placingNulls) {
        this.placingNulls = placingNulls;
    }
    
    public boolean isPlacingRocks() {
        return placingRocks;
    }
    
    public void setPlacingRocks(boolean placingRocks) {
        this.placingRocks = placingRocks;
    }
    
    public Tile getTile( int index) {
        return tiles[index];
    }

    public void setTile(Tile[] tiles) {
        this.tiles = tiles;
    }
    
    public int getPixels() {
        return pixels;
    }
    //</editor-fold>
    
    private int pixels, rows, columns;
    private ImageIcon rockIcon, critterIcon, bugIcon, flowerIcon;
    private BufferedImage originalCritter, originalFlower, originalBug, originalRock;
    private BufferedImage scaledCritter, scaledFlower, scaledBug, scaledRock;
    private Tile[] tiles;

    public Tile getTileN(int index) {
        return tiles[index];
    }

    public void setTileActor(int index, Actor actor) {
        tiles[index].addOccupant(actor);
    }
    
    
    public Board(int pixels, int rows, int columns) {
        
        originalCritter = ImageOperations.loadImage("src/observertest/hamtaro.png");
        originalFlower = ImageOperations.loadImage("src/observertest/photo_daisy.png");
        originalBug = ImageOperations.loadImage("src/observertest/bug.png");
        originalRock = ImageOperations.loadImage("src/observertest/rock.png");

        scaledCritter = ImageOperations.resize(originalCritter, pixels, pixels);
        scaledFlower = ImageOperations.resize(originalFlower, pixels, pixels);
        scaledBug = ImageOperations.resize(originalBug, pixels, pixels);
        scaledRock = ImageOperations.resize(originalRock, pixels, pixels);
        
        //change this to a class eventually
        rockIcon = new ImageIcon(scaledRock);
        critterIcon = new ImageIcon(scaledCritter);
        bugIcon = new ImageIcon(scaledBug);
        flowerIcon = new ImageIcon(scaledFlower);
        
        this.pixels = pixels;
        this.rows = rows;
        this.columns = columns;
        
        this.setLayout(new GridLayout(rows, columns));
        this.setPreferredSize(new Dimension(pixels*columns, pixels*rows));
        
        generateTiles();
        generateTileNieghbors();
    }
    
    private void generateTiles() {
        tiles = new Tile[rows*columns];
        for (int i = 0; i < (rows*columns); i++){
            tiles[i] = new Tile(i, pixels,  rockIcon, critterIcon, bugIcon, flowerIcon);
            this.add(tiles[i]);
        }
    }
    
    public void addTileListeners( GameListener listener ) {
        for ( Tile t : tiles) {
            t.addListener(listener);
            System.out.println("tile:" + t.getIndex() + " listener added");
        }
    }
    
    public void generateTileNieghbors(){
        System.err.println("generate Tile neighbors called");
        for (Tile t : tiles){
            t.generateNieghbors(rows, columns);
        }
    }
}
